package com.sz.app.oss.i18n;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import java.io.*;


/**
 * Function: 调用百度翻译接口处理国际化语言. <br>
 * Date: 2017年02月07日 16:36 <br>
 * Author: wentao.chang
 */
public class I18NGenarator {
    TestRestTemplate restTemplate = new TestRestTemplate();
    String uri = "http://fanyi.baidu.com/v2transapi";

    String replacement = "XXXXXX-XXXXXX";
    String basePath = this.getClass().getResource("").getPath().split("target")[0];
    String src = basePath + "src/main/resources/i18n/messages.properties";
    String dst_en = basePath + "src/main/resources/i18n/messages_en_US_translate.properties";
    String dst_tw = basePath + "src/main/resources/i18n/messages_zh_HK_translate.properties";

    @Test
    public void translate() throws IOException, InterruptedException, JSONException {

        HttpHeaders headers = new HttpHeaders();
        headers.add("Origin", "http://fanyi.baidu.com");
        headers.add("Referer", "http://fanyi.baidu.com");

        StringBuffer param = new StringBuffer();
        param.append("?from=").append("zh");
        param.append("&transtype=").append("realtime");
        param.append("&simple_means_flag=").append("3");
        param.append("&query=");


        HttpEntity entity = new HttpEntity<>(headers);
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(src)));

        //每次都删除新建翻译文件
        File dst_en_file = new File(dst_en);
        File dst_tw_file = new File(dst_tw);
        if (dst_en_file.exists()) {
            dst_en_file.delete();
        }
        dst_en_file.createNewFile();

        if (dst_tw_file.exists()) {
            dst_tw_file.delete();
        }
        dst_tw_file.createNewFile();

        BufferedWriter en_writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dst_en)));
        BufferedWriter tw_writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dst_tw)));

        String data;
        int count = 1;
        while ((data = br.readLine()) != null) {
            //接口限制 100次休息3秒
            count++;
            if (count % 100 == 0) {
                Thread.sleep(3000);
            }
            if (!data.contains("=")) {
                tw_writer.write(data);
                tw_writer.newLine();

                en_writer.write(data);
                en_writer.newLine();
                continue;
            }
            data = data.replaceAll("\\{", replacement);
            String[] kv = data.split("=");

            //繁体
            String url_cht = uri + param.toString() + kv[1] + "&to=cht";
            ResponseEntity<String> cht = restTemplate.exchange(url_cht, HttpMethod.POST, entity, String.class);

            JSONObject json = new JSONObject(cht.getBody());
            JSONObject trans_result = (JSONObject) json.get("trans_result");
            JSONArray trans_result_data = (JSONArray) trans_result.get("data");
            String trans_result_data_dst = (String) ((JSONObject) trans_result_data.get(0)).get("dst");
            tw_writer.write(kv[0] + "=" + trans_result_data_dst.replaceAll(replacement, "{"));
            tw_writer.newLine();
            tw_writer.flush();
            //英文
            String url_en = uri + param.toString() + kv[1] + "&to=en";
            ResponseEntity<String> en = restTemplate.exchange(url_en, HttpMethod.POST, entity, String.class);

            JSONObject json_en = new JSONObject(en.getBody());
            JSONObject trans_result_en = (JSONObject) json_en.get("trans_result");
            JSONArray trans_result_data_en = (JSONArray) trans_result_en.get("data");
            String trans_result_data_dst_en = (String) ((JSONObject) trans_result_data_en.get(0)).get("dst");
            en_writer.write(kv[0] + "=" + trans_result_data_dst_en.replaceAll(replacement, "{"));
            en_writer.newLine();
            en_writer.flush();
        }

        br.close();
        tw_writer.close();
        en_writer.close();
    }


}
